package com.haiyou.dc.mq.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.haiyou.common.util.R;
import com.haiyou.dc.mq.entity.Movie;
import com.haiyou.dc.mq.service.MovieService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author root
 * @date 2020-01-05 11:30:55
 */
@RestController
@RequestMapping("/movie")
@Slf4j
@Api(tags = "电影")
public class MovieController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private Environment env;

    @ApiOperation(value = "查看数据")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) throws JsonProcessingException {
        Movie movie = movieService.selectByPrimaryKey(id);
        rabbitTemplate.setExchange(env.getProperty("mq.basic.info.exchange"));
        rabbitTemplate.setRoutingKey(env.getProperty("mq.basic.info.routeKey"));
        Message build = MessageBuilder.withBody(objectMapper.writeValueAsBytes(movie)).build();
        rabbitTemplate.convertAndSend(build);
        return R.ok(movie);
    }

}